package com.tencent.ncnnyolox.ui

import android.os.Bundle
import android.view.KeyEvent
import android.view.inputmethod.EditorInfo
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView.LayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.tencent.ncnnyolox.R
import com.tencent.ncnnyolox.adapter.ChatRecordAdapter
import com.tencent.ncnnyolox.basemodule.base.BaseActivity
import com.tencent.ncnnyolox.basemodule.router.RouterPath
import com.tencent.ncnnyolox.basemodule.utils.MMKey
import com.tencent.ncnnyolox.basemodule.utils.MMkvUtils
import com.tencent.ncnnyolox.databinding.ActivityCommonRecordBinding
import com.tencent.ncnnyolox.net.ApiViewModel
import com.tencent.ncnnyolox.net.bean.request.ChatHistoryRequest
import com.tencent.ncnnyolox.net.bean.request.OrderByItem

/**
 * <pre>
 *     author : yh
 *     e-mail : xxx@xx
 *     time   : 2024/01/03
 *     desc   : 聊天记录
 * </pre>
 */
@Route(path = RouterPath.CommonWord_ChatRecordActivity)
class CommonChatRecordActivity :BaseActivity<ApiViewModel,ActivityCommonRecordBinding>(){


    private val TAG:String="CommonChatRecordActivity"
    private lateinit var adapter: ChatRecordAdapter

    private var pageIndex=1;
    override fun layoutId(): Int {
        return R.layout.activity_common_record
    }

    override fun initView(savedInstanceState: Bundle?) {

    }

    override fun initData() {
        val bean= ChatHistoryRequest(OrderByItem("",true,""),1,10, MMkvUtils.decodeString(MMKey.memberId),mBinding.etChat.text.toString());
        viewModel.getChatRecord(bean).observe(this){
            adapter= ChatRecordAdapter(it.data)
            mBinding.recycleChatRecord.layoutManager=LinearLayoutManager(this)
            mBinding.recycleChatRecord.adapter=adapter
        }

        //刷新
        mBinding.smartRefresh.setOnRefreshListener {
            val loadMoreBean=ChatHistoryRequest(OrderByItem("",true,""),1,10, MMkvUtils.decodeString(MMKey.memberId),mBinding.etChat.text.toString())
            viewModel.getChatRecord(loadMoreBean).observe(this){
                adapter.setData(it.data)
                adapter.notifyDataSetChanged()
            }
        }

        mBinding.smartRefresh.setOnLoadMoreListener {
            val loadMoreBean=ChatHistoryRequest(OrderByItem("",true,""),pageIndex++,10, MMkvUtils.decodeString(MMKey.memberId),mBinding.etChat.text.toString())
            viewModel.getChatRecord(loadMoreBean).observe(this){
                adapter.setData(it.data)
                adapter.notifyDataSetChanged()
            }
        }

       // 设置软键盘的ActionListener
        mBinding.etChat.setOnEditorActionListener { _, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_DONE || (event != null && event.action == KeyEvent.ACTION_DOWN && event.keyCode == KeyEvent.KEYCODE_ENTER)) {
                // 在这里执行确定按钮被点击时的操作
                handleDoneAction()
                return@setOnEditorActionListener true
            }
            false
        }

        mBinding.ivSearch.setOnClickListener{
            handleDoneAction()
        }

        mBinding.title.leftView.setOnClickListener {
            finish()
        }
    }

    private fun handleDoneAction() {
        //查找聊天记录接口
        val bean= ChatHistoryRequest(OrderByItem("",true,""),1,10, MMkvUtils.decodeString(MMKey.memberId),mBinding.etChat.text.toString());
        viewModel.getChatRecord(bean).observe(this){
            adapter= ChatRecordAdapter(it.data)
            mBinding.recycleChatRecord.layoutManager=LinearLayoutManager(this)
            mBinding.recycleChatRecord.adapter=adapter
        }
    }

}